package com.example.lin.zyfypt_327ljd.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.example.lin.zyfypt_327ljd.R;
import com.example.lin.zyfypt_327ljd.bean.LoginBean;
import com.example.lin.zyfypt_327ljd.iface.LoginListener;
import com.example.lin.zyfypt_327ljd.model.LoginModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btn1,btn2;
    private EditText uEt,pEt;
    private Switch sw;
    private SharedPreferences sp;
    private String username="",password="",sessionid="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toast.makeText(this,"登录", Toast.LENGTH_SHORT).show();//?
        init();
        sp=getSharedPreferences("login",MODE_PRIVATE);
        readSP();
    }
    private LoginListener loginListener=new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean)
        {
            System.out.println("SessionID： "+loginBean.getSessionid());
            if(loginBean.getSessionid()!=null){
                sessionid=loginBean.getSessionid().toString();
                saveSP();
                //Intent intent1 = new Intent();
                // intent1.setClass(LoginActivity.this, MainActivity.class);//forward为当前activity，forwardTarget为新的activity
                Intent intent1 = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent1);
            }
        }
        //        @Override
//        public void onFail(String msg) {
//
//            Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
//        }
        @Override
        public void onFail(String msg) {
            System.out.println("fail");
        }
    };
    private void init(){
        btn1=(Button)findViewById(R.id.button);
        btn2=(Button)findViewById(R.id.button2);
        uEt=(EditText)findViewById(R.id.editText_u);
        pEt=(EditText)findViewById(R.id.editText_p);
        sw=(Switch) findViewById(R.id.switch1);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }
    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putString ("sessionid",sessionid);
        editor.putBoolean("remember",sw.isChecked());
        editor.commit();//提交保存
    }
    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        String session=sp.getString("sessionid",null);

        Boolean b=sp.getBoolean("remember",false);
        if(b) {
            uEt.setText(name);
            pEt.setText(pass);
            sw.setChecked(b);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.button:
                System.out.println("登录");
                username=uEt.getText().toString();
                password=pEt.getText().toString();
                LoginModel loginModel=new LoginModel();
                loginModel.getResult(uEt.getText().toString(),pEt.getText().toString(),loginListener);
                break;
            case R.id.button2:
                System.out.println("注册");
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
        }
    }
}
